{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Pull Portfolio Thematic Analytics with GS Quant\n",
    "\n",
    "## Permission Prerequisites\n",
    "\n",
    "To execute all the code in this tutorial, you will need the following application scopes:\n",
    "- **read_product_data**\n",
    "- **read_financial_data**\n",
    "- **run_analytics** (must be requested)\n",
    "\n",
    "## Step 1: Authenticate and Initialize Your Session\n",
    "\n",
    "First you will import the necessary modules and add your client id and client secret."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import datetime as dt\n",
    "from IPython.display import display\n",
    "\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.session import GsSession, Environment\n",
    "import pandas as pd\n",
    "\n",
    "client = None\n",
    "secret = None\n",
    "\n",
    "## External users must fill in their client ID and secret below and comment out the line below\n",
    "\n",
    "# client = 'ENTER CLIENT ID'\n",
    "# secret = 'ENTER CLIENT SECRET'\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id=client, client_secret=secret)\n",
    "\n",
    "print('GS Session initialized.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Get Portfolio Thematic Report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "thematic_report = PortfolioManager('ENTER YOUR PORTFOLI ID').get_thematic_report()\n",
    "\n",
    "print(f'Thematic report found with ID: {thematic_report.id}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Current Thematic Exposure to All Baskets\n",
    "\n",
    "Once your thematic report is scheduled as of the latest business day, you can view your portfolio's current exposure and\n",
    "beta to every flagship basket in our thematic factor model in just a few lines of code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "thematic_exposures = thematic_report.get_all_thematic_exposures(\n",
    "    start_date=thematic_report.latest_end_date, end_date=thematic_report.latest_end_date\n",
    ")\n",
    "\n",
    "pd.set_option('display.max_colwidth', 0)\n",
    "\n",
    "display(thematic_exposures)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Get Thematic Exposure Breakdown for a Basket\n",
    "\n",
    "Interested in a more granular breakdown of your exposure to a particular basket? Pull your thematic breakdown by asset\n",
    "on a desired date:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "date = dt.date(\n",
    "    2024, 12, 12\n",
    ")  # If date is not in range of portfolio's position data there will not be any thematic data returned.\n",
    "\n",
    "thematic_breakdown = thematic_report.get_thematic_breakdown(date, \"BASKET'S MARQUEE ID\")\n",
    "\n",
    "display(thematic_breakdown)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Historical Thematic Exposure\n",
    "\n",
    "You can also pull the historical change in your thematic exposure to a basket:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "historical_exposures = thematic_report.get_all_thematic_exposures(\n",
    "    start_date=thematic_report.earliest_start_date,\n",
    "    end_date=thematic_report.latest_end_date,\n",
    "    basket_ids=[\"BASKET'S MARQUEE ID\"],\n",
    ")[['Date', 'Thematic Exposure']]\n",
    "\n",
    "\n",
    "historical_exposures.plot(title='Historical Exposure to GSXUSTAY')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
